import os
import sys
import rtconfig

if os.getenv('RTT_ROOT'):
    RTT_ROOT = os.getenv('RTT_ROOT')
else:
    RTT_ROOT = os.path.normpath(os.getcwd() + '/../../../modules/rt-thread')

sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')]
try:
    from building import *
except:
    print('Cannot found RT-Thread root directory, please check RTT_ROOT')
    print(RTT_ROOT)
    exit(-1)

AddOption('--vehicle',
        dest = 'vehicle',
        type='string',
        nargs=1,
        action = 'store',
        default = "",
        help = 'Specify the type of vehicle that needs to be compiled,E.g copter,plane')

TARGET = 'microbee_apgdstv2.' + rtconfig.TARGET_EXT

DefaultEnvironment(tools=[])
env = Environment(tools = ['mingw'],
    AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
    CC = rtconfig.CC, CCFLAGS = rtconfig.CFLAGS,
    AR = rtconfig.AR, ARFLAGS = '-rc',
    CXX = rtconfig.CXX, CXXFLAGS = rtconfig.CXXFLAGS,
    LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS)
env.PrependENVPath('PATH', rtconfig.EXEC_PATH)

if rtconfig.PLATFORM in ['iccarm']:
    env.Replace(CCCOM = ['$CC $CCFLAGS $CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS -o $TARGET $SOURCES'])
    env.Replace(ARFLAGS = [''])
    env.Replace(LINKCOM = env["LINKCOM"] + ' --map microbee_apgdstv2.map')

Export('RTT_ROOT')
Export('rtconfig')

SDK_ROOT = os.path.abspath('./../common/soc/gd32/v1/')

if os.path.exists(SDK_ROOT + os.path.dirname('libraries')):
    libraries_path_prefix = SDK_ROOT + os.path.dirname('/libraries/')

SDK_LIB = libraries_path_prefix
Export('SDK_LIB')

# prepare building environment
objs = PrepareBuilding(env, RTT_ROOT, has_libcpu=False)

stm32_library = 'STM32F4xx_HAL'
rtconfig.BSP_LIBRARY_TYPE = stm32_library

# include libraries
objs.extend(SConscript(os.path.join(libraries_path_prefix, stm32_library, 'SConscript'), variant_dir='build/mb/STM32F4xx_HAL', duplicate=0))

# include drivers
objs.extend(SConscript(os.path.join(libraries_path_prefix, 'HAL_Drivers', 'SConscript'), variant_dir='build/mb/HAL_Drivers', duplicate=0))

#define MICROBEE_BUILD_Rover      1
#define MICROBEE_BUILD_Copter     2
#define MICROBEE_BUILD_Plane      3
#define MICROBEE_BUILD_UNKNOWN    5
#define MICROBEE_BUILD_Sub        7
#define MICROBEE_BUILD_Blimp      12
#define MICROBEE_BUILD_Heli       13

pilot_vdir = 'build/mb/app/'

if GetOption('vehicle') == 'rover':
    AddDepend('MB_VEHICLE_ROVER')
    pilot_vdir = 'build/mb/rover/'
elif  GetOption('vehicle') == 'copter':
    AddDepend('MB_VEHICLE_COPTER')
    pilot_vdir = 'build/mb/copter/'
elif GetOption('vehicle') == 'plane':
    AddDepend('MB_VEHICLE_PLANE')
    pilot_vdir = 'build/mb/plane/'
elif GetOption('vehicle') == 'sub':
    AddDepend('MB_VEHICLE_SUB')
    pilot_vdir = 'build/mb/sub/'
elif GetOption('vehicle') == 'blimp':
    AddDepend('MB_VEHICLE_BLIMP')
    pilot_vdir = 'build/mb/blimp/'
elif GetOption('vehicle') == 'heli':
    AddDepend('MB_VEHICLE_HELI')
    pilot_vdir = 'build/mb/heli/'
else:
    print('Please determine the vehicle type for compiling or generating the project')
    print('E.g, vehicle=rover,copter,plane,sub,blimp,heli')
    print('E.g, scons --vehicle=copter -j4')
    print('E.g, scons --vehicle=copter --target=mdk5')
    exit(-1)

# include pilot libraries
objs.extend(SConscript(os.path.join('./../common/', 'startup', 'SConscript'), variant_dir='build/mb/startup', duplicate=0))
objs.extend(SConscript(os.path.join(RTT_ROOT, '..', '..', 'SConscript'), variant_dir=pilot_vdir, duplicate=0))

# make a building
DoBuilding(TARGET, objs)
